﻿using Google.Apis.Plus.v1.Data;
using Newtonsoft.Json;

namespace Google.Apis.Plus.v1.Data
{
    using System;
    using System.IO;
    using System.Net;
    using System.Web.Script.Serialization;

    public enum PeopleActivityType
    {
        plusoners,
        resharers
    }

    public class GooglePlusAPIHelper
    {
        private string _apiKey;
        private string _pageId;

        public GooglePlusAPIHelper(string pageId, string apiKey)
        {
            this._pageId = pageId;
            this._apiKey = apiKey;
        }

        public Activity GetActivity(string activityId)
        {
            string requestString = "https://www.googleapis.com/plus/v1/activities/" + activityId + "?key=" + this._apiKey;
            string webData = this.GetWebData(requestString);
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            return JsonConvert.DeserializeObject<Activity>(webData);
        }

        public Comment GetComment(string commentId)
        {
            string requestString = "https://www.googleapis.com/plus/v1/comments/" + commentId + "?key=" + this._apiKey;
            string webData = this.GetWebData(requestString);
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            return JsonConvert.DeserializeObject<Comment>(webData);
        }

        public Person GetPerson()
        {
            string requestString = "https://www.googleapis.com/plus/v1/people/" + this._pageId + "?key=" + this._apiKey;
            string webData = this.GetWebData(requestString);
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            return JsonConvert.DeserializeObject<Person>(webData);
        }

        private string GetWebData(string requestString)
        {
            using (StreamReader reader = new StreamReader(WebRequest.Create(requestString).GetResponse().GetResponseStream()))
            {
                return reader.ReadToEnd();
            }
        }

        public ActivityFeed ListActivities()
        {
            return this.ListActivities("");
        }

        public ActivityFeed ListActivities(string nextPageToken)
        {
            string requestString = "https://www.googleapis.com/plus/v1/people/" + this._pageId + "/activities/public?maxResults=20&pageToken=" + nextPageToken + "&key=" + this._apiKey;
            string webData = this.GetWebData(requestString);
            return JsonConvert.DeserializeObject<ActivityFeed>(webData);
        }

        public CommentFeed ListComments(string activityId)
        {
            return this.ListComments(activityId, "");
        }

        public CommentFeed ListComments(string activityId, string nextPageToken)
        {
            string requestString = "https://www.googleapis.com/plus/v1/activities/" + activityId + "/comments?pageToken=" + nextPageToken + "&key=" + this._apiKey;
            string webData = this.GetWebData(requestString);
            return JsonConvert.DeserializeObject<CommentFeed>(webData);
        }

        public PeopleFeed ListPeopleByActivity(string activityId, PeopleActivityType activityType)
        {
            return this.ListPeopleByActivity(activityId, activityType, "");
        }

        public PeopleFeed ListPeopleByActivity(string activityId, PeopleActivityType activityType, string nextPageToken)
        {
            string requestString = "https://www.googleapis.com/plus/v1/activities/" + activityId + "/people/" + activityType.ToString().ToLower() + "&pageToken=" + nextPageToken + "&key=" + this._apiKey;
            string webData = this.GetWebData(requestString);
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            return serializer.Deserialize<PeopleFeed>(webData);
        }


        public ActivityFeed SearchActivities(string query, string language = null, string nextPageToken = "")
        {
            string requestString = "https://www.googleapis.com/plus/v1/activities?maxResults=20&query=" + query + "&pageToken=" + nextPageToken + "&key=" + this._apiKey;
            if (!String.IsNullOrEmpty(language))
            {
                requestString += "&language=" + language;
            }
            string webData = this.GetWebData(requestString);
            return JsonConvert.DeserializeObject<ActivityFeed>(webData);
        }

        public PeopleFeed SearchPeople(string query)
        {
            return this.SearchPeople(query, "");
        }

        public PeopleFeed SearchPeople(string query, string nextPageToken)
        {
            string requestString = "https://www.googleapis.com/plus/v1/people?query=" + query + "&pageToken=" + nextPageToken + "&key=" + this._apiKey;
            string webData = this.GetWebData(requestString);
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            return JsonConvert.DeserializeObject<PeopleFeed>(webData);
        }
    }
}

